package com.szhuang.mobilesafe.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.szhuang.mobilesafe.R;
import com.szhuang.mobilesafe.ui.SettingItemView;

/**
 * Created by szhuang on 2015/3/23.
 */
public class SetUpSecondPage extends GestureDetectorActivity{

    SharedPreferences sp;
    TelephonyManager telephonyManager;
    ImageView last,next;
    SettingItemView bound;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setup_second_page);
        init();
        setListener();
    }



    public void init(){

        telephonyManager= (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        sp=getSharedPreferences("config",MODE_PRIVATE);
        last=(ImageView)findViewById(R.id.backward);
        next= (ImageView) findViewById(R.id.forward);
        bound=(SettingItemView)findViewById(R.id.bound);
        bound.setTitle("点击绑定SIM卡");

        String sim_info=sp.getString("sim",null);
        if(TextUtils.isEmpty(sim_info)){

            bound.setDesc("SIM卡没有绑定");
            bound.setChecked(false);
        }else{
            bound.setDesc("SIM卡已经绑定");
            bound.setChecked(true);
        }

    }

    public void setListener(){

        last.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                previousStep();
            }
        });

        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(bound.isOpen()){

                    nextStep();
                }else{
                    Toast.makeText(SetUpSecondPage.this,"手机卡还没有绑定",Toast.LENGTH_LONG).show();
                }
            }
        });

        bound.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences.Editor editor=sp.edit();
                if(bound.isOpen()){
                    bound.setChecked(false);
                    bound.setDesc("手机卡没有绑定");
                    editor.putString("sim",null);
                }else{
                    bound.setChecked(true);
                    bound.setDesc("手机卡已经绑定");
                    String sim_info=telephonyManager.getSimSerialNumber();
                    editor.putString("sim",sim_info);
                }
                editor.commit();
            }
        });
    }

    @Override
    public void previousStep() {

        Intent intent=new Intent(SetUpSecondPage.this,SetUpFirstPage.class);
        startActivity(intent);
        finish();
        overridePendingTransition(R.anim.tran_pre_in, R.anim.tran_pre_out);
    }

    @Override
    public void nextStep() {

        Intent intent=new Intent(SetUpSecondPage.this,SetUpThirdPage.class);
        startActivity(intent);
        finish();
        overridePendingTransition(R.anim.tran_in, R.anim.tran_out);
    }
}
